<?php

namespace app\api\controller;

use app\common\controller\Api;

/**
 * 首页接口
 */
class Index extends Api
{
    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];

    public function agreement()
    {
        $data = [
            'user_agreement'=>\app\common\model\Config::get(['name'=>'user_agreement'])->value,
            'privacy_protocol'=>\app\common\model\Config::get(['name'=>'privacy_protocol'])->value,
        ];
        $this->success('请求成功',$data);
    }

    //抢单开关
    public function switch()
    {
        $rob_switch = \app\common\model\Config::get(['name'=>'rob_switch'])->value;
        $this->success('请求成功',$rob_switch);
    }

    //预约显示天数
    public function day()
    {
        $rob_switch = \app\common\model\Config::get(['name'=>'day'])->value;
        $this->success('请求成功',$rob_switch);
    }

    //用户修改/取消的弹框
    public function bullet()
    {
        $edit_where = ['edit_type'=>1,'is_edit'=>1,'is_edit_read'=>0];

        $cancel_where = ['cancel_type'=>1,'is_cancel'=>1,'is_read'=>0];

        $edit_list = \app\common\model\Order::alias('o')
            ->join('store s','o.store_id = s.store_id')
            ->join('store_project sp','o.project_id = sp.project_id')
            ->join('user su','o.staff_id = su.id','left')
            ->where($edit_where)
            ->field('o.order_id,o.order_no,o.last_name,o.first_name,o.phone,o.edit_time,
            s.store_name,sp.project_name,su.nickname as staff_name')
            ->order('o.createtime desc')
            ->select();
        $new_edit_list = [];
        foreach ($edit_list as $k=>$v){
            $content = $v['last_name'].$v['first_name'].'修改了订单,订单编号为:'.$v['order_no'];
            $new_edit_list[] = [
                'order_id'=>$v['order_id'],
                'time'=>$v['edit_time'],
                'content'=>$content
            ];
        }
        $cancel_list = \app\common\model\Order::alias('o')
            ->join('store s','o.store_id = s.store_id')
            ->join('store_project sp','o.project_id = sp.project_id')
            ->join('user su','o.staff_id = su.id','left')
            ->where($cancel_where)
            ->field('o.order_id,o.order_no,o.last_name,o.first_name,o.phone,o.cancel_time,
            s.store_name,sp.project_name,su.nickname as staff_name')
            ->order('o.createtime desc')
            ->select();
        $new_cancel_list = [];
        foreach ($cancel_list as $k=>$v){
            $content = $v['last_name'].$v['first_name'].'取消了订单,订单编号为:'.$v['order_no'];
            $new_cancel_list[] = [
                'order_id'=>$v['order_id'],
                'time'=>$v['cancel_time'],
                'content'=>$content
            ];
        }
        //$arr = array_merge($new_edit_list,$new_cancel_list);
        $data = [
            'edit_list'=>$new_edit_list,
            'cancel_list'=>$new_cancel_list
        ];
        $this->success('请求成功',$data);
    }

    //修改弹框订单的已读
    public function editread()
    {
        $edit_id = input('edit_order_id','');

        $cancel_id = input('cancel_order_id','');

        if ($edit_id){
            \app\common\model\Order::where('order_id','in',$edit_id)->update(['is_edit_read'=>1]);
        }

        if ($cancel_id){
            \app\common\model\Order::where('order_id','in',$cancel_id)->update(['is_read'=>1]);
        }
        $this->success('请求成功');
    }


    /**
     * 首页
     *
     */
    public function index()
    {
        $this->success('请求成功');
    }
}
